package View;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.html.ImageView;

import DAO.LoginDAO;

public class LoginFrame extends JFrame {
	public static JPanel contentPane = new JPanel();
	private JTextField userField;
	private JPasswordField pwdField;
	private String role = "";
	static Image logo;
	ImageIcon imageIcon;
	JLabel jLabel;
	String name = null;

	public LoginFrame() {
		setResizable(false);
		setTitle("学生成绩管理系统登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 462, 462);
		setLocationRelativeTo(null);

		contentPane.setLayout(null);
		setContentPane(contentPane);


		// logo
		try {
			logo= ImageIO.read(new File("/Users/mac/Desktop/java大作业学生信息管理/源代码/src/image/logo.jpg"));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//原图缩放
		int width = 100; //缩小到100点像素
		int height = logo.getHeight(null)*100/logo.getWidth(null);//按比例，将高度缩减
		Image smallImg =logo.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		imageIcon =new ImageIcon(smallImg);
		jLabel=new JLabel(imageIcon,SwingConstants.CENTER);
		jLabel.setSize(120,120);
		jLabel.setLocation(180,0);

		contentPane.add(jLabel);



		
		JLabel tip=new JLabel("温馨提示：学生请使用学号登录！");
		tip.setBounds(131, 410,200, 15);
		contentPane.add(tip);
		

		JLabel label_Type = new JLabel("用户类型：");
		label_Type.setForeground(new Color(200, 0, 10));
		label_Type.setFont(new Font("宋体", Font.PLAIN, 20));
		label_Type.setBounds(100, 120, 106, 20);
		contentPane.add(label_Type);

		String[] type = { "请选择用户类型", "学生", "教师", "管理员" };
		final JComboBox comboBox = new JComboBox(type);

		/*添加Item监听，判断登录类型*/
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				Object obj = e.getItem();
				role = obj.toString();
			}
		});

		comboBox.setFont(new Font("楷体", Font.PLAIN, 17));
		comboBox.setBackground(Color.WHITE);
		comboBox.setBounds(200, 120, 151, 25);
		contentPane.add(comboBox);

		JLabel label_userName = new JLabel("用户名：");
		label_userName.setForeground(new Color(200, 0, 10));
		label_userName.setFont(new Font("宋体", Font.PLAIN, 20));
		label_userName.setBounds(120, 180, 80, 20);
		contentPane.add(label_userName);

		userField = new JTextField();
		userField.setColumns(10);
		userField.setBounds(200, 180, 150, 25);
		contentPane.add(userField);

		JLabel label_Pwd = new JLabel("密  码：");
		label_Pwd.setForeground(new Color(200, 0, 10));
		label_Pwd.setFont(new Font("宋体", Font.PLAIN, 20));
		label_Pwd.setBounds(120, 240, 80, 20);
		contentPane.add(label_Pwd);

		pwdField = new JPasswordField();
		pwdField.setBounds(200, 240, 150, 25);
		/*添加按键监听，回车进行登录*/
		pwdField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER)
					try {	//try语句块是正常情况下执行的代码
						login();
						setVisible(false);
					} catch (Exception e1) {	//捕捉并处理异常
					}
			}
		});
		contentPane.add(pwdField);

		JButton button_Login = new JButton("登录");
		/*添加按钮监听，点击按钮进行登录*/
		button_Login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					login();
					setVisible(false);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		button_Login.setBounds(130, 300, 80, 45);
		contentPane.add(button_Login);

		JButton button_Exit = new JButton("取消");
		/*添加按钮监听，点击取消退出程序*/
		button_Exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		button_Exit.setBounds(260, 300, 80, 45);
		contentPane.add(button_Exit);
		setVisible(true);
	}

	public void login() throws Exception {
		/*获取输入的用户名和密码*/
		String user = userField.getText().trim();
		userField.setText("");
		String password = pwdField.getText().trim();
		pwdField.setText("");
		/*调用LoginDAO，与数据库中数据进行比对 ，进行登录*/
		new LoginDAO(role, user, password);
		setVisible(false);
	}
}